import { createRouter, createWebHashHistory } from 'vue-router'

// 导入核心页面组件
import Login from "../views/Login.vue";
import Home from "../views/Home.vue";
import Register from "../views/Register.vue";
import Patient from "../views/Patient.vue";
import Telemedicine from "../views/Telemedicine.vue";
import Mailbox from "../components/Mailbox.vue";
import Cellphone from "../components/Cellphone.vue";

// 创建路由对象
const router = createRouter({
    /**
     * 使用hash模式创建路由历史记录
     * 特点：URL中会带#号，兼容性好，不需要服务器额外配置
     */
    history: createWebHashHistory(),

    // 定义精简后的路由表
    routes: [
        // 认证相关路由
        {
            path: '/mailbox',
            name: 'Mailbox',
            component: Mailbox,
            meta: { title: '我的邮箱' }
        },
        {
            path: '/cellphone',
            name: 'Cellphone',
            component: Cellphone,
            meta: { title: '手机信息' }
        },
        // 莫认跳转
        {
            path: "/",
            redirect: "/home"
        },
        // 登录
        {
            path: "/login",
            name: "Login",
            component: Login,
            meta: { requiresAuth: false }
        },
        // 注册
        {
            path: "/register",
            name: "Register",
            component: Register,
            meta: { requiresAuth: false }
        },
        // 首页
        {
            path: "/home",
            name: "Home",
            component: Home,
            meta: { requiresAuth: false }
        },

        // 患者页面路由
        {
            path: "/patient",
            name: "Patient",
            component: Patient,
            meta: {
                requiresAuth: true ,
                showDefault: true //元信息标记
            },
            // 子路由
            children:[
                {
                    // 跳转显示莫认面板
                    path: '', // 空路径表示默认子路由
                    name: 'PatientManagement',
                    component:()=> import('../components/PatientMainContent.vue'),
                    meta:{
                        requiresAuth: true,
                        showDefault: true
                    }
                },
                {
                    //今日预约
                    path: '/today-appointments',
                    name: 'TodayAppointments',
                    component: () => import('../components/panels/TodayAppointments.vue'),
                    meta: { requiresAuth: true }
                },

                {
                    // 急速问诊
                    path: "/telemedicine",
                    name: "Telemedicine",
                    component: Telemedicine,
                    meta: { requiresAuth: true }
                },
                {
                    // 个人信息
                    path:'/patient-management',
                    name:'PatientManagement',
                    component:()=> import('../components/panels/PatientManagement.vue'),
                    meta:{requiresAuth: true}
                },
                // 专业问诊
                {
                    // 西医诊断
                    path:'/western-diagnosis',
                    name:'WesternDiagnosis',
                    component:()=> import('../components/panels/WesternDiagnosis.vue'),
                    meta:{requiresAuth: true}
                },
                {
                    // 中医辨证
                    path:'/tcm-differentiation',
                    name:'TcmDifferentiation',
                    component:()=> import('../components/panels/TcmDifferentiation.vue'),
                    meta:{requiresAuth: true}
                },
                {
                    // 健康评估
                    path:'/health-assessment',
                    name:'HealthAssessment',
                    component:()=> import('../components/panels/HealthAssessment.vue'),
                    meta:{requiresAuth: true}
                },

                // 我的用药
                {
                    // 西药用药
                    path:'/western-medication',
                    name:'WesternMedication',
                    component:()=> import('../components/panels/WesternMedication.vue'),
                    meta:{requiresAuth: true}
                },
                {
                    // 中药用药
                    path:'/tcm-medication',
                    name:'TcmMedication',
                    component:()=> import('../components/panels/TcmMedication.vue'),
                    meta:{requiresAuth: true}
                },

                // 健康档案
                {
                    // 体征记录
                    path:'/vitals',
                    name:'VitalSigns',
                    component:()=> import('../components/panels/VitalSigns.vue'),
                    meta:{requiresAuth: true}
                },
                {
                    // 检查报告
                    path:'/reports',
                    name:'MedicalReports',
                    component:()=> import('../components/panels/MedicalReports.vue'),
                    meta:{requiresAuth: true}
                },

                {
                    // 病历档案
                    path:'/medical-records',
                    name:'MedicalRecords',
                    component:()=> import('../components/panels/MedicalRecords.vue'),
                    meta:{requiresAuth: true}
                },

                {
                    // 急救指南
                    path:'/first-aid',
                    name:'FirstAidGuide',
                    component:()=> import('../components/panels/FirstAidGuide.vue'),
                    meta:{requiresAuth: true}
                },

                {
                    // 附近医院
                    path:'/find-hospital',
                    name:'HospitalFinder',
                    component:()=> import('../components/panels/HospitalFinder.vue'),
                    meta:{requiresAuth: true}
                },

                {
                    // 系统设置
                    path:'system-settings',
                    name:'Settings',
                    component:()=> import('../components/Settings.vue')
                }
            ]
        }


    ]
})

/**
 * 全局前置路由守卫
 * 作用：在路由跳转前进行拦截和验证
 */
router.beforeEach((to, from, next) => {
    const isLoggedIn = localStorage.getItem("username") !== null

    // 放行无需鉴权的路由
    if (to.meta.requiresAuth === false) {
        return next()
    }

    // 检查需要认证的路由
    if (to.meta.requiresAuth && !isLoggedIn) {
        return next('/login')
    }

    if (from.name === 'Login' && to.name === 'Patient') {
        // 强制显示默认页
        return next({name:'PatientManagement'})
    }

    next()
})

export default router
